﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Wuqi.Webdiyer;

namespace PreContractWeb.UserControl
{
    public partial class Pager : System.Web.UI.UserControl
    {
        protected GridView m_SourceGridView;

        public event EventHandler Before_PageChanged;
        public event EventHandler After_PageChanged;

        /// <summary>
        /// Page css property.
        /// </summary>
        public string CssClass
        {
            set
            {
                this.pagecontrol.CssClass = value;
            }
        }

        /// <summary>
        /// Reset the current page index of grid view.
        /// </summary>
        public int GVCurrentPageIndex
        {
            get
            {
                if (ViewState["GVCurrentPageIndex"] == null)
                {
                    return 1;
                }
                else
                {
                    return (int)ViewState["GVCurrentPageIndex"];
                }
            }
            set
            {
                ViewState["GVCurrentPageIndex"] = value;
            }

        }

        /// <summary>
        /// Reset the current page index of grid view.
        /// </summary>
        public bool ResetCurrentPageIndex
        {
            get
            {
                if (ViewState["ResetCurrentPageIndex"] == null)
                {
                    return true;
                }
                else
                {
                    return (bool)ViewState["ResetCurrentPageIndex"];
                }
            }
            set
            {
                ViewState["ResetCurrentPageIndex"] = value;
            }
        }

        /// <summary>
        /// Data source  property of grid view.
        /// </summary>
        private GridView SourceGridView
        {
            get
            {
                if (m_SourceGridView != null)
                {
                    return m_SourceGridView;
                }
                else
                {
                    string m_SourceGridViewName = ViewState["SourceGridViewName"].ToString();
                    Control gridview = this.Parent.FindControl(m_SourceGridViewName);
                    if (gridview != null)
                    {
                        return (GridView)gridview;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            set
            {
                ViewState["SourceGridViewName"] = value.ID;
            }
        }

        ///// <summary>
        ///// Data source  property of grid view.
        ///// </summary>
        //private GridView SourceGridView
        //{
        //    get
        //    {
        //        return m_SourceGridView;

        //    }
        //    set
        //    {
        //        m_SourceGridView = value;
        //    }
        //}

        /// <summary>
        /// Data source.
        /// </summary>
        public DataTable DataSource
        {
            get
            {
                if (DataSourceView != null)
                {
                    return DataSourceView.Table;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Data source of grid view.
        /// </summary>
        public DataView DataSourceView
        {
            get
            {
                if (Session[this.ID + "DataSource"] != null)
                {
                    return (DataView)Session[this.ID + "DataSource"];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                Session[this.ID + "DataSource"] = value;
            }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {

        }

        /// <summary>
        /// Show the page control result information.
        /// </summary>
        private void ShowCostomInfoText()
        {
            int recordcount = this.pagecontrol.RecordCount;
            int pagecount = this.pagecontrol.PageCount;
            int currentpageindex = this.pagecontrol.CurrentPageIndex;
            if (recordcount == 0)
            {
                currentpageindex = 0;

            }
            this.lblpage.Text = "Total Records <font color='Black'><b>" + recordcount.ToString() + "</b></font><img src='../Images/PageIcon.GIF' /> Page <font color='red'><b>" + currentpageindex.ToString() + "</b></font>/<font color='Black'><b>" + pagecount.ToString() + "</b></font>";
            // this.pagecontrol.CustomInfoText = "Total Records <font color='Black'><b>" + recordcount.ToString() + "</b></font><img src='../Images/PageIcon.GIF' /> Page <font color='red'><b>" + currentpageindex.ToString() + "</b></font>/<font color='Black'><b>" + pagecount.ToString() + "</b></font>";
        }

        /// <summary>
        /// Bind data to grid view.
        /// </summary>
        /// <param name="gridview">Grid view id</param>
        /// <param name="datasource">data source</param>
        public void BindtoGridView(GridView gridview, DataView datasource)
        {
            try
            {
                this.SourceGridView = gridview;
                this.DataSourceView = datasource;
                this.pagecontrol.PageSize = SourceGridView.PageSize;
                this.SourceGridView.DataSource = DataSourceView;
                this.SourceGridView.DataBind();
                if (ResetCurrentPageIndex == true)
                {
                    if (SourceGridView.PageCount >= GVCurrentPageIndex)
                    {
                        SourceGridView.PageIndex = GVCurrentPageIndex - 1;

                    }
                    else
                    {

                        SourceGridView.PageIndex = 0;
                    }
                }

                if (datasource != null)
                {
                    this.pagecontrol.RecordCount = datasource.Count;
                    this.pagecontrol.CurrentPageIndex = SourceGridView.PageIndex + 1;

                }
                else
                {
                    this.pagecontrol.RecordCount = 0;
                    this.pagecontrol.CurrentPageIndex = 0;

                }

                ShowCostomInfoText();

                this.SourceGridView.Visible = this.Visible;

                this.lblpage.Visible = this.Visible;
                this.pagecontrol.Visible = this.Visible;


            }
            catch (Exception ex)
            {

                Response.Write("<script defer>alert('绑定出错');</script>");

            }
        }

        public int GetIndex()
        {
            return this.pagecontrol.CurrentPageIndex;
        }

        public int GetPagesize()
        {
            return this.pagecontrol.PageSize;
        }

        public void PageChangeEvent()
        {
            try
            {
                this.pagecontrol.CurrentPageIndex = GVCurrentPageIndex;
                this.SourceGridView.PageIndex = GVCurrentPageIndex - 1;
                this.SourceGridView.DataBind();
                this.ShowCostomInfoText();
            }
            catch
            {

            }
        }

        /// <summary>
        /// Change the page index of grid view.
        /// </summary>
        protected void pagecontrol_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
        {
            try
            {
                if (Before_PageChanged != null)
                {
                    Before_PageChanged(this, new EventArgs());
                }
                this.pagecontrol.CurrentPageIndex = e.NewPageIndex;
                this.SourceGridView.DataSource = DataSourceView;
                this.SourceGridView.PageIndex = this.pagecontrol.CurrentPageIndex - 1;
                GVCurrentPageIndex = e.NewPageIndex;
                this.SourceGridView.DataBind();

                this.ShowCostomInfoText();
                if (After_PageChanged != null)
                {
                    After_PageChanged(this, new EventArgs());
                }
            }
            catch (Exception ex)
            {

                return;
                //Response.Write("<script defer>alert('换页出错');</script>");


            }
        }
    }
}